package com.wqr.customer.controller;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author LTX
 */
@RestController
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("")
    public Object getIndex() {
//        return restTemplate.getForObject("http://localhost:8081/",String.class,"");
        return restTemplate.getForObject("http://hello-server/", String.class, "");
    }


    /**
     * ribbon演示
     */
    @GetMapping("ribbon")
    public Object getRibbon() {
        ServiceInstance choose = loadBalancerClient.choose("hello-server");
        String ip = choose.getHost();
        int port = choose.getPort();
        return restTemplate.getForObject("http://" + ip + ":" + port, String.class, "");
    }
}
